<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="银行卡名称" prop="name">
        <el-input v-model="dataForm.name" maxlength="8"></el-input>
      </el-form-item>
      <el-form-item label="银行卡缩写(字母)" prop="acronym">
        <el-input v-model="dataForm.acronym" placeholder="比如:NY" maxlength="8"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <Imglist @updataImg='updataImg' :limit='1' :value="iconImg" :dialogVisible='visible'></Imglist>
      </el-form-item>
      <el-form-item label="排序号" prop="sort">
        <el-input-number v-model="dataForm.sort" :precision="0" :min="1"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  treeDataTranslate
} from '@/utils'
import Imglist from './../../oss/oss-uploadImg'
import seviceApi from '@/api/serviceApi.js'
export default {
  components: {
    Imglist
  },
  data () {
    return {
      visible: false,
      dialogVisible: false,
      dataForm: {
        id: '0',
        name: '',
        acronym: '',
        icon: [],
        sort: ''
      },
      iconImg: [],
      dataRule: {
        name: [{
          required: true,
          message: '银行卡名称不能为空',
          trigger: 'blur'
        }],
        icon: [{
          required: true,
          message: '图标不能为空'
        }],
        sort: [{
          required: true,
          message: '排序号不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (this.dataForm.id == 0) {
        this.iconImg = []
      }
      this.dataForm.icon = []
    },
    // 上传图片
    updataImg (e) {
      this.dataForm.icon = e
    },
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (Object.prototype.toString.call(this.dataForm.icon) === '[object Array]') {
            this.dataForm.icon = this.dataForm.icon.join(',')
          } else {
            this.dataForm.icon = this.dataForm.icon
          }
          seviceApi.saveBankCardIcon(this.dataForm).then(res => {
            if (res && res.data.code == 200) {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.visible = false
              this.$emit('updataList', 1)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
